<?php
namespace controller;

class Access extends Base {
	
	protected $user = null;
	
	public function __construct()
	{
		parent::__construct();
		session_start();
		if (empty($_SESSION['user']))
		{
			if ($this->isAjaxRequest()) {
				echo '{"code":401,"msg":"未登入"}';
			} else {
				header('Location: /login');
			}
			exit;
		}
		$this->user = $_SESSION['user'];
	}
	
	private function isAjaxRequest()
	{
		return isset($_SERVER['HTTP_X_REQUESTED_WITH']) || (isset($_REQUEST['isAjax']) && $_REQUEST['isAjax']);
	}
}